const glob = require('glob');
const fs = require('fs-extra');

import {
  camelize,
  PROFILE_DIR
} from '@shared';
import {
  isCli
} from '@core/util';

const srcPath = isCli() ? 'src/' : '';

export function canUseShare(pagePath) {
  const filePath = fs.readFileSync(srcPath + pagePath + '.vue', 'utf8');
  return ~filePath.indexOf('onShareAppMessage');
}

export function readRouters() {
  const files = glob.sync(srcPath + '*(pages|platforms)/**/*.json');
  const routers = [];
  let i = 0,
    amount = files.length;
  for (; i < amount; i++) {
    routers.push(fs.readJsonSync(files[i]));
  }
  return routers;
}

export function readProfile() {
  const files = glob.sync(srcPath + PROFILE_DIR + '/*.json');
  let result = {};
  let i = 0,
    amount = files.length,
    option = '';
  for (; i < amount; i++) {
    option = camelize(files[i].match(/([a-z\-]+)\.json/)[1]);
    result[option] = fs.readJsonSync(files[i]);
  }
  return result;
}
